package mapgenerator;

import com.graphics.ImageIdentity;
import com.ip.TilePattern;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
 *
 * @author Sajarwo Anggai
 */
public class WorkSpace implements ActionListener {

    private Main comp;

    public WorkSpace(Main comp) {
        this.comp = comp;
        comp.ToolbarP.addActionListener(this);
        comp.ToolbarS.addActionListener(this);
    }

   

    public static int pixelsToTiles(int pixels, int tileSize) {
        return (int) Math.floor((float) pixels / tileSize);
    }

    public static int tilesToPixels(int numTiles, int tileSize) {
        return numTiles * tileSize;
    }

    private Object currentToolbarButton;
    public void actionPerformed(ActionEvent e) {
       Object ob=e.getSource();
       if(ob == comp.ToolbarP){
           currentToolbarButton = comp.ToolbarP;
       }
    }



    public void renderWorkspaceBottom(String imageName){
        comp.WorkspaceBottom.removeAll();
        int x = 0, y = 0;
        int width = 0, height = 0;
        ImageIdentity ident=comp.ipContainer.getPattern(imageName);
        for (int i = 0; i < ident.getImages().size(); i++) {
            TilePattern tp = new TilePattern(imageName,(BufferedImage) ident.getImages().get(i),i);
            tp.setToolTipText("Pos = "+i);
            tp.setVisible(true);
            if (ident.isSprite()) {
                width = tp.getImage().getWidth();
                height = tp.getImage().getHeight();
            } else {
                width = comp.project.getTileWidth();
                height = comp.project.getTileHeight();
            }
            comp.WorkspaceBottom.add(tp).setBounds(x, y, width, height);
            if ((i + 1) % ident.getCols() == 0) {
                x = 0;
                y += height + comp.ipControlPanel.getVGap();
            } else {
                x += width + comp.ipControlPanel.getHGap();
            }
        }
        x = ident.getCols() * width;
        x += ident.getCols() * comp.ipControlPanel.getHGap();
        y = ident.getRows() * height;
        y += ident.getRows() * comp.ipControlPanel.getVGap();

        comp.WorkspaceBottom.setPreferredSize(new Dimension(x, y));
        comp.WorkspaceBottom.repaint();
        comp.WorkspaceBottom.revalidate();
    }
}
